let globalData = getApp().globalData;
const util = require('../../utils/util.js');
var xrRequest = require("../../vendor/hope-sdk/core/request");
var config = require('../../config');
const app = getApp()
Page({
  data: {
    logined: false,
    authsetting: 0,
    userinfo: null,
    userDetail:null,
    addCarFlag:false,

    height: 64, //header高度
    top: 0, //标题图标距离顶部距离
    scrollH: 0, //滚动总高度
    opcity: 0,
    iconOpcity: 0.5,
    pageIndex: 1,
    loadding: false,
    pullUpOn: true
  },
  onLoad: function (options) {
    var _this = this;
    app.pageOnLoadInit(this,true).then(function (res) {
      //这里写验证登录成功后且无需验证授权 需要执行的逻辑
      //若还需验证授权成功才执行的逻辑需写在onShow方法里面，
      //并且这里pageOnLoadInit()第二个参数要为 true
      console.log(res);
    }, function (error) {
      // 登录失败
      wx.showModal({
        title: "Error",
        content: error.errMsg ? error.errMsg : 'Fail to login.Please feedback to manager.',
      });
      return false;
    });


    let obj = wx.getMenuButtonBoundingClientRect();
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          width: obj.left || res.windowWidth,
          height: obj.top ? (obj.top + obj.height + 8) : (res.statusBarHeight + 44),
          top: obj.top ? (obj.top + (obj.height - 32) / 2) : (res.statusBarHeight + 6),
          scrollH: res.windowWidth * 0.6
        })
      }
    })
  },
  href(e) {
    let page = Number(e.currentTarget.dataset.type)
    let url = "";
    switch (page) {
      case 0:  // 服务我的门店
        url = "../shop/list/list"
        break;
      case 1:
        url = "../order/apply/apply"
        break;
      case 2:
        url = "setting/setting"
        break;
      case 3:
        url = "userinfo/userinfo"
        break;
      case 4:
        url = "../order/myOrder/myOrder"
        break;
      case 7: //我的车辆
        url = "../car/list/list"
        break;
      case 8: //我的轮胎
        url = "../tyre/list/list"
        break;
      case 10:  // 优惠劵
        url = "../coupon/list/list"
        break;
      case 12:
        url = "../rescue/apply/apply"
          break;
      case 13:
        url = "../rescue/my/my"
          break;
      default:
        break;
    }
    if (url && this.checkLogin()) {
      wx.navigateTo({
        url: url,
      });
    }
  },
  checkLogin: function () {
    if (!this.data.logined) {  // 未登录
      wx.showModal({
        title: "Error",
        content: 'Fail to login.Please feedback to manager.',
      });
      return false;
    }
    if (this.data.authsetting == 0) {
      wx.navigateTo({
        url: "../auth/auth"
      })
      return false;
    }
    return true;
  },
  detail: function () {
    wx.navigateTo({
      url: '../../productDetail/productDetail'
    })
  },
  tyre:function(){
    wx.navigateTo({
      url: '../tyre/list/list'
    })
  },
  onPageScroll(e) {
    let scroll = e.scrollTop <= 0 ? 0 : e.scrollTop;
    let opcity = scroll / this.data.scrollH;
    if (this.data.opcity >= 1 && opcity >= 1) {
      return;
    }
    this.setData({
      opcity: opcity,
      iconOpcity: 0.5 * (1 - opcity < 0 ? 0 : 1 - opcity)
    })
  },
  onPullDownRefresh() {
    setTimeout(() => {
      wx.stopPullDownRefresh()
    }, 200)
  },
  onReachBottom: function () {
    if (!this.data.pullUpOn) return;
    this.setData({
      loadding: true
    })
    // if (this.data.pageIndex == 4) {
    //   this.setData({
    //     loadding: false,
    //     pullUpOn: false
    //   })
    // } else {
    //   let loadData = JSON.parse(JSON.stringify(this.data.productList));
    //   loadData = loadData.splice(0, 10)
    //   if (this.data.pageIndex == 1) {
    //     loadData = loadData.reverse();
    //   }
    //   this.setData({
    //     loadding: false,
    //     pageIndex: this.data.pageIndex + 1,
    //     productList: this.data.productList.concat(loadData)
    //   })
    // }
  },
  onShow: function () {
    var that = this
    app.pageOnShowInit(that).then(function () {

     // console.log(that.userinfo);
      //这里写验证授权成功后 需要执行的逻辑
       that.initUserInfo();

       if (that.data.addCarFlag){
         wx.navigateTo({
           url: '../car/add/add?source=0'
         })
         that.setData({
           addCarFlag :false
         })
       }
    }, function () {
      //用户没有授权
      console.warn('Auth scope.userInfo is unable.');
      return false;
    });
    // wx.hideTabBar({
    //   success: function () {
    //     app.editTabbar();
    //   }
    // });
    app.checkMessageCount();
  },
  
  initUserInfo: function () {
    var that = this;
    xrRequest.request(app,{
      url: config.service.getUserDetail,
      login: true,
      success: function (res) {
        that.setData({
          userDetail: res.data.data,
        });
      },
      authfail: function (e) {
        wx.navigateTo({
          url: "../auth/auth"
        })
      }
    });
  },
  goToOrder: function (e) {
    let type = Number(e.currentTarget.dataset.type);
    wx.navigateTo({
      url: "../order/myOrder/myOrder?currentTab=" + type
    });
  },
  goToRescue:function(e){
    let type = Number(e.currentTarget.dataset.type);
    wx.navigateTo({
      url: "../rescue/my/my?currentTab=" + type
    });
  },
  callphone:function(e){
    wx.makePhoneCall({
      phoneNumber: '4000339066' //仅为示例，并非真实的电话号码
    })
  }
})